package sortingRMI_client;

import java.io.Serializable;
import java.util.Comparator;

@SuppressWarnings("rawtypes")
public class MuziekCD implements Comparable<MuziekCD>, Serializable {
	
	private static final long serialVersionUID = -1373731126044511626L;
	private String threadName;
	private String artist;
	private String title;
	private int year;


	public MuziekCD() {
		super();
	}
	
	// sort by title
	public static Comparator titleCompare = new Comparator<MuziekCD>() {
		public int compare(MuziekCD CD, MuziekCD otherCD) {
			String title1 = ((MuziekCD) CD).getTitle();
			String title2 = ((MuziekCD) otherCD).getTitle();
			
			return title1.compareTo(title2);
		}
	};

	// sort by artist (falls through to title if artist is the same)
	public static Comparator artistCompare = new Comparator<MuziekCD>() {
		public int compare(MuziekCD CD, MuziekCD otherCD) {
			String artist1 = ((MuziekCD) CD).getArtist();
			String artist2 = ((MuziekCD) otherCD).getArtist();
			
			String title1 = ((MuziekCD) CD).getTitle();
			String title2 = ((MuziekCD) otherCD).getTitle();
			
			if (!(artist1.equals(artist2))) {
				return artist1.compareTo(artist2);
			}
			else {
				return title1.compareTo(title2);
			}
		}
	};
	
	// sort by everything (default)
	public int compareTo(MuziekCD otherCD) {
		final int BEFORE = -1;
		final int EQUAL = 0;
		final int AFTER = 1;

		if ( this == otherCD ) return EQUAL;

		int compareArtist = this.getArtist().compareTo(otherCD.getArtist());
		if ( compareArtist != EQUAL ) return compareArtist;
		
		int compareTitle = this.getTitle().compareTo(otherCD.getTitle());
		if ( compareTitle != EQUAL ) return compareTitle;
		
		if ( this.getYear() < otherCD.getYear() ) return BEFORE;
		if ( this.getYear() > otherCD.getYear() ) return AFTER;
			
		assert this.equals(otherCD);
		return EQUAL;
	}

	public void setArtist(String artist) {
		this.artist = artist;
	}
	
	public String getArtist() {
		return (String) artist;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	
	public String getTitle() {
		return (String) title;
	}
	
	public void setYear(int year) {
		this.year = year;
	}
	
	public int getYear() {
		return year;
	}

	public void setThreadName(String threadName) {
		this.threadName = threadName;
	}

	public String getThreadName() {
		return threadName;
	}
}
